<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../zTree_v3/css/zTreeStyle/zTreeStyle.css" type="text/css">
</head>
<body>
职位名称：<input type="text" name="jobTitle" id="jobTitle">
<ul id="treeDemo" class="ztree"></ul>
<button type="button" id="btn_sub">提交</button>
<script type="text/javascript" src="../bootstrap/vue.js"></script>
<script type="text/javascript" src="../bootstrap/jquery3.6.js"></script>
<script type="text/javascript" src="../bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../bootstrap/my.js"></script>
<script type="text/javascript" src="../zTree_v3/js/jquery.ztree.all.js"></script>
<script>
    //查询菜单列表
    function selMenu(){
        let zNodes;
        $.ajax({
            type:"post",
            url:"../Menu/selMenuAll",
            async:false,
            dataType:"json",
            success:function (json){
                zNodes=json.list;
            }

        })
        return zNodes;
    }
    $(function (){
        $.fn.zTree.init($("#treeDemo"), setting, selMenu());
    })

    var setting = {
        check: {
            enable: true,
            chkboxType:{"Y":"ps","N":"ps"}
        },
        data: {
            simpleData: {
                enable: true,
                idKey:"menuId",
                pIdKey:"menuPid",
                rootPid:0
            },
            key:{
                name:"menuName",
            },
            // callback: {
            //     onClick: zTreeOnClick
            // }
        }
    };
    $("#btn_sub").click(function (){
        fun_getCheckValue()
    })
    function  fun_getCheckValue(){
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        var nodes=zTree.getChangeCheckedNodes(true);
        var result='';
        if(nodes.length==0){
            alert("请选择部门！！");
            return false;
        }
        for (var i = 0; i < nodes.length; i++) {
            var halfCheck = nodes[i].getCheckStatus();
            if (!halfCheck.half){
                result += nodes[i].menuId +',';
            }

        }
        result=result.substring(0,result.lastIndexOf(","));
        var jobTitle=$("#jobTitle").val();
        $.ajax({
            type:"post",
            url:"../Menu/addMenu",
            data: {
                "result":result,
                "jobTitle":jobTitle,
            },
            dataType: "json",
            success:function (json){
                if (json.message == "添加成功") {
                    location.href = "job_list.html";
                }
            }
        })
    }
    // function zTreeOnClick(event, treeId, treeNode) {
    //     console.log(treeNode);
    //     console.log(treeNode.menuId);
    //     console.log(treeNode.menuPid);
    //     console.log(treeNode.menuName);
    // };
</script>
</body>
</html>